package com.rxyb.security.service.logic;

import cn.hutool.json.JSONUtil;
import cn.rxyb.core.date.DateTime;
import cn.rxyb.core.date.enums.FormatDate;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import static com.rxyb.security.constants.DbConstant.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rxyb.security.constants.DbConstant;
import com.rxyb.security.db.dao.SysUserMapper;
import com.rxyb.security.db.po.SysMenuResouce;
import com.rxyb.security.db.po.SysUser;
import com.rxyb.security.gate.model.AddMenuRoleReq;
import com.rxyb.security.gate.model.bean.MenuListBean;
import com.rxyb.security.gate.model.bean.UserInfoBean;
import com.rxyb.security.gate.model.common.PageReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author YJH
 * @version 1.0
 * @description 用户逻辑
 * @date 2020/7/11 17:28
 */
@Component
@Slf4j
public class SysUserLogic {


    private final SysUserMapper sysUserMapper;

    public SysUserLogic(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }


    /**
     * 请求参数
     *
     * @param user 请求信息
     * @return 响应参数
     */
    public boolean add(SysUser user) {
        log.debug("sysUser --> add--> params:[{}]", JSONUtil.toJsonStr(user));
        user.setAddTime(DateTime.getDate());
        user.setUpdateTime(DateTime.getDate());
        user.setRowStatus(ROW_STATE_VALID);
        return sysUserMapper.insert(user) > 0 ? true : false;
    }


    /**
     * 更新信息
     *
     * @param user 参数
     * @return 响应参数
     */
    public boolean update(SysUser user) {
        log.debug("sysUser --> add--> params:[{}]", JSONUtil.toJsonStr(user));
        user.setUpdateTime(DateTime.getDate());
        user.setRowStatus(ROW_STATE_VALID);

        return sysUserMapper.updateById(user) > 0 ? true : false;
    }


    /**
     * 更新信息
     *
     * @param user      参数
     * @param condition 条件参数
     * @return 响应参数
     */
    public boolean update(SysUser user, SysUser condition) {
        log.debug("sysUser --> add--> params:[{}]", JSONUtil.toJsonStr(user));
        user.setUpdateTime(DateTime.getDate());
        user.setRowStatus(ROW_STATE_VALID);
        return sysUserMapper.update(user, new Logic().condition(condition)) > 0 ? true : false;
    }


    /**
     * 根据对象查询单个信息
     *
     * @param id
     * @return
     */
    public SysUser findById(Long id) {
        log.debug("sysUser --> findById--> params:[{}]", id);
        return sysUserMapper.selectById(id);
    }


    /**
     * 根据对象查询列表信息
     *
     * @param id
     * @return
     */
    public List<SysUser> findUserList(SysUser user) {
        log.debug("sysUser --> findUserList--> params:[{}]", JSONUtil.toJsonStr(user));
        return sysUserMapper.selectList(new Logic().condition(user));
    }


    /**
     * 分页信息
     *
     * @param user 用户信息
     * @param page 分页参数
     * @return
     */
    public IPage<SysUser> findUserPage(SysUser user, PageReq page) {
        log.debug("sysUser --> findUserPage--> params:[{}]", JSONUtil.toJsonStr(user));
        IPage<SysUser> userIPage = new Page<>(page.getCurrentPage(), page.getPageSize());
        return sysUserMapper.selectPage(userIPage, new Logic().condition(user));
    }


    /**
     * db 转换成页面需要得数据
     *
     * @param resouce
     * @return
     */
    public UserInfoBean dbToModel(SysUser resouce) {
        UserInfoBean bean = new UserInfoBean();
        BeanUtils.copyProperties(resouce, bean);
        bean.setAddTime(DateTime.getDateFormat(resouce.getAddTime(), FormatDate.FORMAT_TIME_HOUR_MINUTE_SECOND));
        bean.setUpdateTime(DateTime.getDateFormat(resouce.getUpdateTime(), FormatDate.FORMAT_TIME_HOUR_MINUTE_SECOND));
        return bean;
    }


    /**
     * 逻辑内部接口处理信息
     */
    private class Logic {

        /**
         * 条件查询信息
         *
         * @param sysUser 参数信息
         * @return
         */
        public QueryWrapper<SysUser> condition(SysUser sysUser) {
            QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(sysUser.getUsername())) {
                wrapper.lambda().eq(SysUser::getUsername, sysUser.getUsername());
            }
            return wrapper;
        }
    }

}
